using System;
using System.Collections;

namespace Sedna.Core.Support
{
	/// <summary>
	/// convenience utility class that is used for checking arguments and throwing exceptions
	/// if their value is not correct
	/// </summary>
	public class Assert
	{
		protected Assert()
		{
		}

		public static void IsTrue(bool val, string msg)
		{
			if(!val)
			{
				throw new SednaException(msg);
			}
		}

		public static void IsFalse(bool val, string msg)
		{
			IsTrue(!val, msg);
		}

	    /// <summary>
	    /// Throws exception of the specified type if <code>val</code> is false
	    /// </summary>
	    public static void IsTrue(bool val, string msg, Type exceptionType)
        {
            if (!val)
            {
                throw (Exception)exceptionType.GetConstructor(Type.EmptyTypes).Invoke(new object[] {msg});
            }
        }

	    /// <summary>
        /// Throws exception of the specified type if <code>val</code> is true
	    /// </summary>
	    public static void IsFalse(bool val, string msg, Type exceptionType)
        {
            IsTrue(!val, msg, exceptionType);
        }

        public static void IsNull(object obj, string msg)
        {
            if (obj != null)
            {
                throw new SednaException(msg);
            }
        }
	    
		public static void IsNotNull(object obj, string msg)
		{
			if(obj == null)
			{
				throw new SednaException(msg);
			}
		}

		public static void IsNotZero(int val, string msg)
		{
			if(val == 0)
			{
				throw new SednaException(msg);
			}
		}

		public static void IsNotEmptyString(string val, string msg)
		{
			IsNotNull(val, msg);
			if(val == string.Empty)
			{
				throw new SednaException(msg);
			}
		}

		public static void IsNotEmpty(ICollection col, string msg)
		{
			IsNotNull(col, msg);
			if(col.Count == 0)
			{
				throw new SednaException(msg);
			}
		}
	}
}
